package zhoufu.sz.sanxiao_check.presents;

import java.net.ConnectException;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import zhoufu.sz.sanxiao_check.models.CheckItemModel;
import zhoufu.sz.sanxiao_check.models.DangerModel;
import zhoufu.sz.sanxiao_check.net.RefreshRepository;
import zhoufu.sz.sanxiao_check.net.RequestHelper;
import zhoufu.sz.sanxiao_check.ui.adapter.items.FireSafeChildItem;
import zhoufu.sz.sanxiao_check.ui.adapter.items.FireSafeHeaderItem;
import zhoufu.sz.sanxiao_check.ui.contract.RefreshContract;

/**
 * 项目： SanXiao_Check
 * 时间： 2016/6/27 17:14
 * 人员： hyc
 * 功能：  的P层
 */
public class RefreshRecyclerViewPresenter implements RefreshContract.Persenter {
    public static final String KEY_NAME = "Key_Name";
    public static final String KEY_SheQuID = "Key_SheQuID";
    public static final String KEY_WangGeID = "Key_WangGeID";
    public static final String KEY_LouDongID = "Key_LouDongID";
    public static final String KEY_sysType = "KEy_sysType";
    public static final String KEY_IsCheck = "KEY_IsCheck";

    private final RefreshRepository _refreshRepository;
    private RefreshContract.View _view;
    private int _sysType;
    private String _grade;

    private int _pageIndex;

    @Inject
    public RefreshRecyclerViewPresenter(RefreshRepository repository) {
        _refreshRepository = repository;
    }

    public void bindV(RefreshContract.View view) {
        _view = view;
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onDestroy() {

    }


    @Override
    public boolean hasMore() {
        return false;
    }

    public void pullData(String sid) {
        _refreshRepository.GetLastExamine(sid, new RequestHelper.RequestCallBack<CheckItemModel>() {
            @Override
            public void onPullStart() {

            }

            @Override
            public void onPullFailud(Throwable throwable) {
                if (throwable.getCause() instanceof ConnectException){
                    _view.connectWrong();
                    return;
                }
                _view.pullFaild();
                System.out.println("throwable.getMessage()) = " + throwable.getMessage());
            }

            @Override
            public void onPullEnd() {
                _view.pullEnd();
            }

            @Override
            public void onPullSuccessed(CheckItemModel s) {

                CheckItemModel checkItemModel = s.getData().get(0);
                comData(checkItemModel);
            }
        });
    }

    public void comData(CheckItemModel checkItemModel) {
        List<DangerModel> dangers = checkItemModel.getDangers();
        ArrayList<FireSafeHeaderItem> headerItems=new ArrayList<>();
        for (DangerModel danger : dangers) {
            FireSafeHeaderItem item = new FireSafeHeaderItem();
            item.setDB(danger);
            FireSafeChildItem childItem=new FireSafeChildItem();

            childItem.setDB(danger);
            childItem.setHeader(item);
            item.addSubItem(childItem);

            headerItems.add(item);
        }
        _view.pullSuccesed(headerItems,checkItemModel);
    }
}
